package com.dubbo.CarBalance.service;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.dubbo.CarBalance.AccountCarerService;
import com.dubbo.CarBalance.mapper.AccountCarerMapper;
import com.dubbo.CarBalance.mapper.AccountDeatilMapper;
import com.dubbo.common.dtos.AppHttpCodeEnum;
import com.dubbo.common.dtos.ResponseResult;
import com.dubbo.common.exception.CarServiceException;
import com.dubbo.model.balance.dtos.AccountCarerDto;
import com.dubbo.model.balance.dtos.AccountDetailDto;
import com.dubbo.model.balance.pojos.AccountCarer;
import com.dubbo.model.balance.pojos.AccountDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Service
@Component
public class AccountCarerServiceImpl implements AccountCarerService {

    @Autowired
    private AccountCarerMapper mapper;

    @Autowired
    private AccountDeatilMapper deatilMapper;

    @Override
    public ResponseResult<List<AccountCarer>> list(AccountCarerDto dto) {
        if (dto==null){
           throw new CarServiceException(AppHttpCodeEnum.PARAM_INVALID);
        }

        List<AccountCarer> list = mapper.findCarerList(dto);

        if (CollectionUtils.isNotEmpty(list)){
            return ResponseResult.okResult(list);
        }

        return ResponseResult.okResult(null);
    }

    @Override
    public ResponseResult<List<AccountCarer>> allList() {
        List<AccountCarer> list = mapper.AllCarerList();

        if (list!=null){
            return ResponseResult.okResult(list);
        }
        return ResponseResult.okResult(null);
    }

    @Override
    public ResponseResult<AccountDetailDto> detail(String id) {
        //参数校验
        if (id == null){
            throw new CarServiceException(AppHttpCodeEnum.PARAM_INVALID);
        }

        AccountDetailDto detailDto = new AccountDetailDto();

        //查询车主账单明细
        AccountCarer accountCarer = mapper.findById(id);
        if (accountCarer!=null){
            detailDto.setAccountId(accountCarer.getAccountId());
            detailDto.setDigest(accountCarer.getDigest());
            detailDto.setPrice(accountCarer.getPrice());
            detailDto.setRemark(accountCarer.getRemark());
        }

        //查询车主账单详情
        AccountDetail accountDetail = deatilMapper.findbyAccountId(id);
        if (accountDetail!=null){
            detailDto.setAccountDetail(accountDetail);
        }

        return ResponseResult.okResult(detailDto);
    }
}
